from jsmodel_bistable_feedback_switch_draw import *
from pylab import plot
from pylab import show
from pylab import legend
from pylab import figure
from pylab import semilogy

import datetime
import os
dir = "./beef/"
fname = dir + (sys.argv[0].split('.')[0] + "/"+ "".join(str(datetime.datetime.now()).split())).replace(':','').replace('.','').replace('-','') + "_" + str(sys.argv[0]) + "_log.txt"
pname = dir + (sys.argv[0].split('.')[0] + "/"+ "".join(str(datetime.datetime.now()).split())).replace(':','').replace('.','').replace('-','') + "_" + str(sys.argv[0]) + "_plot"
print datetime.datetime.now()
fp = open(fname, 'w')


############
#DESCRIPTION
############
#Preklop 2->1 (mCITRIN -> NEPTUN)
#Testiramo, kdaj moramo vstaviti pristinamicin in koliko casa mora biti prisoten, da bo preklop uspesen


##############
#CONFIGURATION
##############
#Bifurcation parameter (general)
BIF_P1 = linspace(1000,7000,120)
BIF_P2 = linspace(10,2000,120)
#Labels
sxlabel = 'Pristinamicyn input time [min]'
sylabel = 'Pristinamicyn duration [min]'


###########
#SIMULATION
###########

fa = figure()
i = 1
r_old = 0;
for bbp1 in BIF_P1:
  for bbp2 in BIF_P2:
 
    bp1 = int(bbp1)
    bp2 = int(bbp2)
    
    t = 10000
    p1 = 200
    p2 = 200
    p3 = 200
    p4 = 200
    p5 = 200
    pcs = [bp1]
    pchl = 0
    pcd  = [bp2]
    rms = [0]
    rmhl = 0
    rmd  = [500]
    thresh = 1e2
    req = False
    #Hill coefficient: inducer 1, inducer2, tal97, tal57
    hk = [2.0,2.0,3.0,3.0]
    switch = BistableFeedbackSwitch(True, t, p1, p2, p3, p4, p5, req, pcs, pchl, pcd, rms, rmhl, rmd, hk, thresh)
    r = switch.run()
    #r = int(random()*4)-1
            
    if not r == r_old:
      lstring = "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15} *\n"
      lstring = lstring.format(t, p1, p2, p3, p4, p5, pcs, req, pchl, pcd, rms, rmhl, rmd, hk, thresh, r)
    else:
      lstring = "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15} \n"
      lstring = lstring.format(t, p1, p2, p3, p4, p5, pcs, req, pchl, pcd, rms, rmhl, rmd, hk, thresh, r)
    fp.write(lstring)
    
    r_old = r;
    
    i = i + 1
    if i % 1000 == 0:
      print i
    
    if r == 0:
      plot(bp1,bp2, 'gs', label='BISTABLE')
    elif r == 1:
      plot(bp1,bp2, 'rs', label='MONOSTATE 57')
    elif r == 2:
      plot(bp1,bp2, 'bs', label='MONOSTATE 97')
    elif r == 3:
      plot(bp1,bp2, 'ys', label='AMBIGOUS')
    elif r == -1:
      plot(bp1,bp2, 'ks', label='NOT VALID')
    

fp.close()
xlabel(sxlabel)  
ylabel(sylabel)
savefig(pname+".png", format ='png')
savefig(pname+".svg", format ='svg')
print datetime.datetime.now()
quit()

